package com.peng.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.edu.entity.Course;
import com.peng.edu.entity.vo.CourseInfoVo;
import com.peng.edu.entity.vo.CoursePublishVo;
import com.peng.edu.entity.vo.CourseQuery;
import com.peng.edu.service.CourseService;
import com.peng.utils.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-16
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //添加课程基本信息方法
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id= courseService.addCourseInfo(courseInfoVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        return Result.ok().data(map);
    }

    //根据id查询课程信息
    @GetMapping("/getCourseInfoId/{courseId}")
    public Result getCourseInfoId(@PathVariable String courseId){
        CourseInfoVo courseInfoVo= courseService.getCourseInfoId(courseId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("courseInfoVo",courseInfoVo);
        return Result.ok().data(map);
    }

    //修改课程表单
    @PutMapping("/updateCourseInfo")
    @CacheEvict(value = "index",allEntries = true)
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.addCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getpublishCourseInfo/{id}")
    public Result getpublishCourseInfo(@PathVariable String id){
        CoursePublishVo publishCourseInfo = courseService.getPublishCourseInfo(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("publishCourse",publishCourseInfo);
        return Result.ok().data(map);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    @CacheEvict(value = "index",allEntries = true)
    public Result publishCourse(@PathVariable String id){
        Course course = courseService.getById(id);
        course.setStatus("Normal");
        courseService.updateById(course);
        return Result.ok();
    }

    //课程列表 基本实现
    @GetMapping("/getCourseList")
    public Result getCourseList(){
        List<Course> list = courseService.list(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        return Result.ok().data(map);
    }

    //多条件查询课程带分页
    @ApiOperation(value = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public Result pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Integer page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Integer limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {//通过封装courseQuery对象来直接传递查询条件
        Page<Course> pageCourse = new Page<>(page,limit);
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(courseQuery.getTitle()!=null ,Course::getTitle,courseQuery.getTitle());
        queryWrapper.eq(courseQuery.getStatus()!=null ,Course::getStatus,courseQuery.getStatus());
        queryWrapper.ge(courseQuery.getBegin()!=null ,Course::getGmtCreate,courseQuery.getBegin());
        queryWrapper.le(courseQuery.getEnd()!=null ,Course::getGmtModified,courseQuery.getEnd());
        queryWrapper.orderByDesc(Course::getGmtCreate);
        IPage<Course> courseIPage = courseService.page(pageCourse,queryWrapper);
        return Result.ok().data(courseIPage);
    }

    //课程列表中删除课程方法
    @DeleteMapping("/removeCourseById/{id}")
    @CacheEvict(value = "index",allEntries = true)
    public Result removeCourseById(@PathVariable String id){
        boolean flag=courseService.removeCourse(id);
        return flag? Result.ok():Result.error();
    }
}

